﻿using Mangox.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading.Tasks;

namespace Mangox.DAL
{
    public class StockDAL
    {

        //锁定最好是私有的object类型
        private static readonly object synObj = new object();

        /// <summary>
        /// 读数实时监控表的所有股票代码
        /// </summary>
        /// <returns></returns>
        public DataTable GetRealTimeMonitoringList()
        {
            DbHelperAccess dbh = new DbHelperAccess();
            DataTable dt = null;
            lock (synObj)
            {
                //DataTable dt = dbh.GetInfoDateSet("select * from 实时监控 where Type<>1 and Type<>4").Tables[0];
                dt = dbh.GetInfoDateSet("select * from 实时监控").Tables[0];                
            }
            return dt;
        }


        /// <summary>
        /// 读Access数据库的股票代码，包括指数
        /// 腾讯代码，前端显示，button List
        /// type：1自选股，2大盘
        /// </summary>
        /// <returns></returns>
        public DataTable GetTencentStockList(int type)
        {
            DbHelperAccess dbh = new DbHelperAccess();
            DataTable dt = null;
            lock (synObj)
            {
                if (type == 1)
                {
                    dt = dbh.GetInfoDateSet("select * from 实时监控 where Type=3 or Type=0").Tables[0];                    
                }
                else if (type == 2)
                {
                    dt = dbh.GetInfoDateSet("select * from 实时监控 where Type=1 or Type=2 or Type=3 or Type=4 or Type=5").Tables[0];                    
                }
            }
            return dt;
        }

        /// <summary>
        /// 读数据库的股票代码，包括指数
        /// 新浪代码，提醒监控
        /// </summary>
        /// <returns></returns>
        public DataTable GetSinaStockList()
        {
            DbHelperAccess dbh = new DbHelperAccess();
            DataTable dt = null;
            lock (synObj)
            {
                dt = dbh.GetInfoDateSet("select * from 实时监控 where Type=1 or Type=2 or Type=3 or Type=5 or Type=0 order by ID asc").Tables[0];
            }
            return dt;
        }

        /// <summary>
        /// 根据列名称查询数据
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetColumnByID(string columnName, int id)
        {
            DbHelperAccess dbh = new DbHelperAccess();
            DataTable dt = null;
            lock (synObj)
            {
                dt = dbh.GetInfoDateSet("select "+ columnName +" from 实时监控 where ID="+ id +"").Tables[0];
            }
            return dt;
        }

        /// <summary>
        /// 清理提醒Log
        /// </summary>
        public int ClearLog()
        {
            DbHelperAccess dbh = new DbHelperAccess();
            string sql = "update 实时监控 set 涨跌幅Log='',涨跌到价Log='',梯形数Log=''";
            int num = 0;
            lock (synObj)
            {
                num = dbh.ExecuteCommand(sql);
            }
            return num;
        }


        /// <summary>
        /// 操作数据库
        /// </summary>
        /// <param name="sql">操作sql</param>
        public int ExecuteCommand(string sql)
        {
            DbHelperAccess dbh = new DbHelperAccess();
            int num = 0;
            lock (synObj)
            {
                num = dbh.ExecuteCommand(sql);
            }
            return num;
        }

        /// <summary>
        /// 测试写入
        /// </summary>
        public int InsertDBTest(int i)
        {
            DbHelperAccess dbh = new DbHelperAccess();
            int num = 0;
            string sql = "insert into 实时监控(名称,代码,梯形幅,Type) values('test','666666',"+ i +",0)";
            num = dbh.ExecuteCommand(sql);
            //System.Threading.Thread.Sleep(500);

            string sql1 = "update 实时监控 set 梯形数Log='+" + i + ",' where ID=102";
            num = dbh.ExecuteCommand(sql1);
            return num;
        }


        //public async Task TaskProducer(ConcurrentQueue<Func<int>> queue)
        //{
        //    for (int i = 1; i <= 20; i++)
        //    {
        //        await Task.Delay(50);
        //        var workItem = new Custom { Id = i };
        //        queue.Enqueue(workItem);//入列  
        //        Console.WriteLine("Task {0} has been posted", workItem.Id);
        //    }
        //}  



    }
}
